package action.creditapproval;

import java.sql.Timestamp;
import java.util.List;

import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import model.creditcheck.Applyopinion;
import model.creditcheck.Applystate;
import service.applycheck.ApplyOpinionService;
import service.applycheck.ApplyStateService;
import service.credit.CreditApplyEnterpriseService;
import service.credit.CreditApplyPersonalService;
import util.Log;
import util.Time;
import vo.CreditCheckBean;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class CreditFinalApprovalAction extends BaseAction{
	private String state;
	private int type;
	private List<?> creditFinalApprovalBeanList;
	private CreditCheckBean creditCheckBean;
	private List<?> recordList;
	
	private Integer applyid;
	private Integer appid;
	private int isCanChange;
	
	private Integer setcheckstate;
	private String setopinion;
	
	private ApplyStateService applyStateService;
	private CreditApplyPersonalService creditApplyPersonalService;
	private CreditApplyEnterpriseService creditApplyEnterpriseService;
	private ApplyOpinionService applyOpinionService;
	
	private int searchwhat;
	
	public String execute() throws Exceptions{
		if(this.type == 1){
			this.creditFinalApprovalBeanList = this.creditApplyPersonalService.getAllFinalApprovalList("wait");
		}else if(this.type == 2){
			this.creditFinalApprovalBeanList = this.creditApplyEnterpriseService.getAllFinalApprovalList("wait");
		}
		this.searchwhat = 1;
		return "success";
	}
	
	public String searchList() throws Exceptions{
		if(this.type == 1){
			this.creditFinalApprovalBeanList = this.creditApplyPersonalService.getAllAcceptionListByApplyIdAndState(this.applyid, "finalApproval");
		}else if(this.type == 2){
			this.creditFinalApprovalBeanList = this.creditApplyEnterpriseService.getAllAcceptionListByApplyIdAndState(this.applyid, "finalApproval");
		}
		return "success";
	}
	
	public String applyFinalApproval() throws Exceptions{
		if (this.type == 1) {
			creditCheckBean = this.creditApplyPersonalService.getOneBean(applyid);
			recordList = this.applyOpinionService.getListAllByApplyId(applyid);
		} else if (this.type == 2) {
			creditCheckBean = this.creditApplyEnterpriseService.getOneBean(applyid);
			recordList = this.applyOpinionService.getListAllByApplyId(applyid);
		}
		return "detail";
	}
	
	public void doFinalApprovalPersonal() throws Exception{
		String result = "";
		Timestamp nowTime = Time.CurrentTimestamp();
		Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(appid);
		Applystate _applystate = applyStateService.getOneApplyStateByIdAndType(appid, "D");
		try {
			if(this.setcheckstate == 1){
				result = "同意";
				_creditapplypersonal.setCreditstate(10);
				_creditapplypersonal.setIsdone(4);
			}else if(this.setcheckstate == 2){
				result = "复议";
				if(_creditapplypersonal.getCount() >= 2){
					_creditapplypersonal.setCreditstate(40);
				}else{
					_creditapplypersonal.setCreditstate(32);
					_creditapplypersonal.setIsdone(4);
				}
			}else if(this.setcheckstate == 12){
				result = "不同意";
				_creditapplypersonal.setCreditstate(40);
			}
			_applystate.setCheckopinion(setopinion);
			_applystate.setCheckstate(setcheckstate);
			_applystate.setChecktime(nowTime);
			creditApplyPersonalService.save(_creditapplypersonal);
			applyStateService.save(_applystate);
			this.applyOpinionService.save(new Applyopinion(appid, Global.userid().intValue(), null, "终审", setopinion, result, nowTime, this.type));
			Log.save("插入", "个人终审，申请单号码为：" + appid, "操作人员为：" + Global.userid());
		} catch (Exception e) {
			throw new Exceptions("在执行过程中出现了错误了!");
		}
	}
	
	public void doFinalApprovalEnterprise() throws Exceptions{
		String result = "";
		Timestamp nowTime = Time.CurrentTimestamp();
		Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(appid);
		Applystate _applystate = applyStateService.getOneApplyStateByIdAndType(appid, "D");
		try {
			if(this.setcheckstate == 1){
				result = "同意";
				_creditapplyenterprise.setCreditstate(10);
				_creditapplyenterprise.setIsdone(4);
			}else if(this.setcheckstate == 2){
				result = "复议";
				if(_creditapplyenterprise.getCount() >= 2){
					_creditapplyenterprise.setCreditstate(40);
				}else{
					_creditapplyenterprise.setCreditstate(32);
					_creditapplyenterprise.setIsdone(4);
				}
			}else if(this.setcheckstate == 12){
				result = "不同意";
				_creditapplyenterprise.setCreditstate(40);
			}
			_applystate.setCheckstate(setcheckstate);
			_applystate.setCheckopinion(setopinion);
			_applystate.setChecktime(nowTime);
			creditApplyEnterpriseService.save(_creditapplyenterprise);
			applyStateService.save(_applystate);
			this.applyOpinionService.save(new Applyopinion(appid, Global.userid().intValue(), null, "终审", setopinion, result, nowTime, this.type));
			Log.save("插入", "企业终审，申请单号码为：" + appid, "操作人员为：" + Global.userid());
		} catch (Exception e) {
			throw new Exceptions("在执行过程中出现了错误了!");
		}
	}
	
	public String doFinalApproval() throws Exception{
		if(this.type == 1){
			this.doFinalApprovalPersonal();
		}else if(this.type == 2){
			this.doFinalApprovalEnterprise();
		}else{
			throw new Exceptions("你的操作存在问题，请按照要求来！");
		}
		
		//this.applyOpinionService.save(new Applyopinion(appid, Global.userid().intValue(), null, "终审", setopinion, result, nowTime, this.type));
		MessageJump("信息终审完成", "creditFinalApprovalAction?type=" + this.type);
		return "message";
	}
	
	/*public String doFinalApproval() throws Exception{
		String result = "";
		Timestamp nowTime = Time.CurrentTimestamp();
		if(this.type == 1){
			Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(appid);
			Applystate _applystate = applyStateService.getOneApplyStateByIdAndType(appid, "D");
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					_creditapplypersonal.setCreditstate(10);
					_creditapplypersonal.setIsdone(4);
				}else if(this.setcheckstate == 2){
					result = "不同意";
					if(_creditapplypersonal.getCount() >= 2){
						_creditapplypersonal.setCreditstate(40);
					}else{
						_creditapplypersonal.setCreditstate(32);
						_creditapplypersonal.setIsdone(4);
					}
				}
				_applystate.setCheckopinion(setopinion);
				_applystate.setCheckstate(setcheckstate);
				_applystate.setChecktime(nowTime);
				creditApplyPersonalService.save(_creditapplypersonal);
				applyStateService.save(_applystate);
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}else if(this.type == 2){
			Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(appid);
			Applystate _applystate = applyStateService.getOneApplyStateByIdAndType(appid, "D");
			try {
				if(this.setcheckstate == 1){
					result = "同意";
					_creditapplyenterprise.setCreditstate(10);
					_creditapplyenterprise.setIsdone(4);
				}else if(this.setcheckstate == 2){
					result = "不同意";
					if(_creditapplyenterprise.getCount() >= 2){
						_creditapplyenterprise.setCreditstate(40);
					}else{
						_creditapplyenterprise.setCreditstate(32);
						_creditapplyenterprise.setIsdone(4);
					}
				}
				_applystate.setCheckstate(setcheckstate);
				_applystate.setCheckopinion(setopinion);
				_applystate.setChecktime(nowTime);
				creditApplyEnterpriseService.save(_creditapplyenterprise);
				applyStateService.save(_applystate);
			} catch (Exception e) {
				throw new Exceptions("在执行过程中出现了错误了!");
			}
		}
		this.applyOpinionService.save(new Applyopinion(appid, Global.userid().intValue(), null, "终审", setopinion, result, nowTime, this.type));
		MessageJump("信息审查完成", "creditFinalApprovalAction?type=" + this.type);
		return "message";
	}*/
	
	public String cdw() throws Exceptions{
		if(this.type == 1){
			this.creditFinalApprovalBeanList = this.creditApplyPersonalService.getAllFinalApprovalList("wait");
		}else if(this.type == 2){
			this.creditFinalApprovalBeanList = this.creditApplyEnterpriseService.getAllFinalApprovalList("wait");
		}
		this.searchwhat = 1;
		return "success";
	}
	/*public String cdd() throws Exceptions{
		if(this.type == 1){
			this.creditFinalApprovalBeanList = this.creditApplyPersonalService.getAllFinalApprovalList("doing");
		}else if(this.type == 2){
			this.creditFinalApprovalBeanList = this.creditApplyEnterpriseService.getAllFinalApprovalList("doing");
		}
		this.searchwhat = 2;
		return "success";
	}*/
	
	public String cdb() throws Exceptions{
		if(this.type == 1){
			this.creditFinalApprovalBeanList = this.creditApplyPersonalService.getAllFinalApprovalList("back");
		}else if(this.type == 2){
			this.creditFinalApprovalBeanList = this.creditApplyEnterpriseService.getAllFinalApprovalList("back");
		}
		this.searchwhat = 3;
		return "success";
	}
	
	/*public String cdr() throws Exceptions{
		if(this.type == 1){
			this.creditFinalApprovalBeanList = this.creditApplyPersonalService.getAllFinalApprovalList("returnBack");
		}else if(this.type == 2){
			this.creditFinalApprovalBeanList = this.creditApplyEnterpriseService.getAllFinalApprovalList("returnBack");
		}
		this.searchwhat = 4;
		return "success";
	}*/
	
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public List<?> getcreditFinalApprovalBeanList() {
		return creditFinalApprovalBeanList;
	}

	public void setcreditFinalApprovalBeanList(List<?> creditFinalApprovalBeanList) {
		this.creditFinalApprovalBeanList = creditFinalApprovalBeanList;
	}

	public CreditCheckBean getCreditCheckBean() {
		return creditCheckBean;
	}
	public void setCreditCheckBean(CreditCheckBean creditCheckBean) {
		this.creditCheckBean = creditCheckBean;
	}
	public ApplyStateService getApplyStateService() {
		return applyStateService;
	}
	public void setApplyStateService(ApplyStateService applyStateService) {
		this.applyStateService = applyStateService;
	}
	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}
	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}
	public CreditApplyEnterpriseService getCreditApplyEnterpriseService() {
		return creditApplyEnterpriseService;
	}
	public void setCreditApplyEnterpriseService(
			CreditApplyEnterpriseService creditApplyEnterpriseService) {
		this.creditApplyEnterpriseService = creditApplyEnterpriseService;
	}

	public List<?> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<?> recordList) {
		this.recordList = recordList;
	}

	public ApplyOpinionService getApplyOpinionService() {
		return applyOpinionService;
	}

	public void setApplyOpinionService(ApplyOpinionService applyOpinionService) {
		this.applyOpinionService = applyOpinionService;
	}

	public int getIsCanChange() {
		return isCanChange;
	}

	public void setIsCanChange(int isCanChange) {
		this.isCanChange = isCanChange;
	}

	public Integer getApplyid() {
		return applyid;
	}

	public void setApplyid(Integer applyid) {
		this.applyid = applyid;
	}

	public Integer getAppid() {
		return appid;
	}

	public void setAppid(Integer appid) {
		this.appid = appid;
	}

	public Integer getSetcheckstate() {
		return setcheckstate;
	}

	public void setSetcheckstate(Integer setcheckstate) {
		this.setcheckstate = setcheckstate;
	}

	public String getSetopinion() {
		return setopinion;
	}

	public void setSetopinion(String setopinion) {
		this.setopinion = setopinion;
	}

	public List<?> getCreditFinalApprovalBeanList() {
		return creditFinalApprovalBeanList;
	}

	public void setCreditFinalApprovalBeanList(List<?> creditFinalApprovalBeanList) {
		this.creditFinalApprovalBeanList = creditFinalApprovalBeanList;
	}

	public int getSearchwhat() {
		return searchwhat;
	}

	public void setSearchwhat(int searchwhat) {
		this.searchwhat = searchwhat;
	}
}
